export const LANG = 'lang'
import LangENUS from './en-us/index'
import LangZHCN from './zh-cn/index'
import { createI18n } from 'vue-i18n'
import { Locale } from 'vant'
import enUS from 'vant/es/locale/lang/en-US'
import zhCN from 'vant/es/locale/lang/zh-CN'

type langType = 'zh-cn' | 'en-us'

export const setVantLocale = (val: string) => {
  // vant 组件库语言配置
  let vantLocale = 'zh-CN'
  let resources = zhCN
  if (val === 'en-us') {
    vantLocale = 'en-US'
    resources = enUS
  }
  Locale.use(vantLocale, resources)
}

export const getLang = function () {
  return sessionStorage.getItem(LANG) || 'zh-cn'
}
export const setLang = function (val: langType) {
  setVantLocale(i18n.global.locale)
  i18n.global.locale = val
  return sessionStorage.setItem(LANG, val)
}
export const getLangType = function () {
  return sessionStorage.getItem(LANG) == 'zh-cn' ? 1 : 2
}

const i18n = createI18n({
  locale: getLang() || 'zh-cn',
  messages: {
    'en-us': LangENUS,
    'zh-cn': LangZHCN
  }
})
setVantLocale(i18n.global.locale)
export default i18n
